from pglite.model import meta
from pglite.model import User

from md5 import md5
from datetime import datetime

class UserManager:

    def newValidationKey(self, login):
        time = datetime.now()
        key = str(time) + login
        
        return md5(key).hexdigest()
        
    def createUser(self, login, password, email):
        theUser = User(login, password, email)
        theUser.validation_key = self.newValidationKey(theUser.login)
        
        meta.Session.add(theUser)
        meta.Session.commit()
        
        return theUser
        
    def validateLogin(self, login):
        users = meta.Session.query(User.id).filter_by(login = login).scalar()
        
        return users == None
        
    def validatePassword(self, login):
        return True
        
    def getUserById(self,id):
        return meta.Session.query(User).filter_by(id = id).first()
